use actix::{Actor, StreamHandler};
use actix_web::ws;

pub struct WsHandler;

impl WsHandler {
    pub fn new() -> Self {
        WsHandler
    }
}

impl Actor for WsHandler {
    type Context = ws::WebsocketContext<Self>;
}

/// Handler for ws::Message message
impl StreamHandler<ws::Message, ws::ProtocolError> for WsHandler {
    fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
        match msg {
            ws::Message::Ping(msg) => {
                println!("ping message: {:?}", msg);
                ctx.pong(&msg)
            },
            ws::Message::Text(text) => {
                println!("text message: {:?}", text);
                ctx.text(text)
            },
            ws::Message::Binary(bin) => {
                println!("binary message: {:?}", bin);
                ctx.binary(bin)
            },
            _ => {
                println!("unknown message");
                ()
            },
        }
    }
}